<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/05/30 07:08:18 $)
// $RCSfile: functions_instaalert.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Fetches the Popup Alerts
*/
function fetchInstaAlertPopups(&$xml, $departmentidlist)
{
	global $_SWIFT, $datastore, $dbCore;

	if (!_is_array($_SWIFT["instaalertcache"]))
	{
		$_SWIFT["instaalertcache"] = array();
	}

	if (empty($_SWIFT["instaalertcache"][$_SWIFT["staff"]["staffid"]]))
	{
		// First Time?
	} else {
		// We have a last fetch date for this. Limit Popups to 10, Show latest first.

		$dbCore->query("SELECT `ticketid`, `subject`, `departmentid` FROM `". TABLE_PREFIX ."tickets` WHERE `departmentid` IN (". buildIN($departmentidlist) .") AND `ticketstatusid` IN (". buildIN(getDisplayTicketStatusList()) .") AND `lastuserreplytime` > '". intval($_SWIFT["instaalertcache"][$_SWIFT["staff"]["staffid"]]) ."' ORDER BY `ticketid` DESC LIMIT 10;");
		while ($dbCore->nextRecord())
		{
			$xml->addParentTag("alert");
				$xml->addTag("title", $_SWIFT["departmentcache"][$dbCore->Record["departmentid"]]["title"].": ".$dbCore->Record["subject"]);
				$xml->addTag("link", $_SWIFT["swiftpath"]."staff/index.php?_m=tickets&_a=viewticket&ticketid=".intval($dbCore->Record["ticketid"]));
			$xml->endParentTag("alert");
		}
	}

	$_SWIFT["instaalertcache"][$_SWIFT["staff"]["staffid"]] = DATENOW;

	$datastore->update("instaalertcache", $_SWIFT["instaalertcache"]);
}
?>